package com.block.api.controller.applet;

import com.alibaba.fastjson.JSONObject;
import com.block.applet.service.AppletConfigInfoService;
import com.block.applet.service.OverdueRecordService;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.security.util.ApiUtil;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.*;
import com.market.member.service.IMemberLoginSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * @author hekelong
 * @date 2025/10/16 下午3:45
 */
@RestController
@RequestMapping("api/applet")
public class OverdueRecordController {

    @Autowired
    OverdueRecordService overdueRecordService;
    @Autowired
    IMemberLoginSessionService memberLoginSessionService;
    @Autowired
    AppletConfigInfoService appletConfigInfoService;

    @RequestMapping(value = "/appOverdueRecord", method = {RequestMethod.POST, RequestMethod.GET})
    public String addOverdueRecord(@RequestAttribute(value = "tranData") JSONObject tranData,
                                   @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                   @RequestAttribute(value = "channelBaseInfo") ChannelBaseInfo channelBaseInfo,
                                   @RequestAttribute(value = "applicationInfo") ApplicationInfo applicationInfo,
                                   @RequestAttribute(value = "uri") String uri,
                                   @RequestAttribute(value = "startDate") Date startdate,
                                   @RequestAttribute(value = "ip") String ip) {
        try {
            String token = tranData.getString("token");
            String uuid = token.substring(token.lastIndexOf("::") + 2);
            String nowDate = LocalDate.now().toString();
            Long uid = memberLoginSessionService.getUidByToken(uuid, nowDate);
            int i = overdueRecordService.addOverdueRecord(tranData, uid);
            if (i == 0) {
                ResponseVo responseVo = new ResponseVo(AppRspCodeConstant.PARAM_ERROR, "添加失败");
                return ApiUtil.dealReturnJson(responseVo, appEncrypt, startdate, uri);
            }
            ResponseVo responseVo = new ResponseVo(AppRspCodeConstant.SUCCESS);
            return ApiUtil.dealReturnJson(responseVo, appEncrypt, startdate, uri);
        } catch (Exception e) {
            // 记录异常日志
            e.printStackTrace();
            // 返回错误响应
            ResponseVo responseVo = new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION, "系统异常");
            return ApiUtil.dealReturnJson(responseVo, appEncrypt, startdate, uri);
        }
    }

    @RequestMapping(value = "/getOverdueRecord", method = {RequestMethod.POST, RequestMethod.GET})
    public String getOverdueRecord(@RequestAttribute(value = "tranData") JSONObject tranData,
                                   @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                   @RequestAttribute(value = "channelBaseInfo") ChannelBaseInfo channelBaseInfo,
                                   @RequestAttribute(value = "applicationInfo") ApplicationInfo applicationInfo,
                                   @RequestAttribute(value = "uri") String uri,
                                   @RequestAttribute(value = "startDate") Date startdate,
                                   @RequestAttribute(value = "ip") String ip) {
        try {
            String token = tranData.getString("token");
            String uuid = token.substring(token.lastIndexOf("::") + 2);
            String nowDate = LocalDate.now().toString();
            Long uid = memberLoginSessionService.getUidByToken(uuid, nowDate);

            // 调用服务层查询逾期记录
            List<OverdueRecord> overdueRecords = overdueRecordService.getOverdueRecordByUserId(uid);

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("overdueRecords", overdueRecords);

            ResponseVo responseVo = new ResponseVo(AppRspCodeConstant.SUCCESS, jsonObject);
            return ApiUtil.dealReturnJson(responseVo, appEncrypt, startdate, uri);
        } catch (Exception e) {
            // 记录异常日志
            e.printStackTrace();
            // 返回错误响应
            ResponseVo responseVo = new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION, "系统异常");
            return ApiUtil.dealReturnJson(responseVo, appEncrypt, startdate, uri);
        }
    }

    @RequestMapping(value = "/getOverdueRecordInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public String getOverdueRecordInfo(@RequestAttribute(value = "tranData") JSONObject tranData,
                                       @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                       @RequestAttribute(value = "channelBaseInfo") ChannelBaseInfo channelBaseInfo,
                                       @RequestAttribute(value = "applicationInfo") ApplicationInfo applicationInfo,
                                       @RequestAttribute(value = "uri") String uri,
                                       @RequestAttribute(value = "startDate") Date startdate,
                                       @RequestAttribute(value = "ip") String ip) {
        try {
            // 调用服务层查询逾期记录
            List<OverdueRecordInfo> overdueRecordInfos = overdueRecordService.getOverdueRecordInfo();

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("overdueRecordsInfo", overdueRecordInfos);

            ResponseVo responseVo = new ResponseVo(AppRspCodeConstant.SUCCESS, jsonObject);
            return ApiUtil.dealReturnJson(responseVo, appEncrypt, startdate, uri);
        } catch (Exception e) {
            // 记录异常日志
            e.printStackTrace();
            // 返回错误响应
            ResponseVo responseVo = new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION, "系统异常");
            return ApiUtil.dealReturnJson(responseVo, appEncrypt, startdate, uri);
        }
    }

    @RequestMapping(value = "/getContext", method = {RequestMethod.POST, RequestMethod.GET})
    public String getContext(@RequestAttribute(value = "tranData") JSONObject tranData,
                             @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                             @RequestAttribute(value = "channelBaseInfo") ChannelBaseInfo channelBaseInfo,
                             @RequestAttribute(value = "applicationInfo") ApplicationInfo applicationInfo,
                             @RequestAttribute(value = "uri") String uri,
                             @RequestAttribute(value = "startDate") Date startdate,
                             @RequestAttribute(value = "ip") String ip) {
        try {
            List<OverdueRecordInfo> overdueRecordInfo = overdueRecordService.getOverdueRecordInfo();
            JSONObject jsonObject = new JSONObject();

            if (overdueRecordInfo != null && !overdueRecordInfo.isEmpty()) {
                StringBuilder sb = new StringBuilder();
                int count = Math.min(overdueRecordInfo.size(), 3);
                for (int i = 0; i < count; i++) {
                    OverdueRecordInfo recordInfo = overdueRecordInfo.get(i);
                    sb.append(", 用户姓名: ").append(recordInfo.getUserName())
                            .append(", 逾期平台: ").append(recordInfo.getOverduePlatform())
                            .append(", 逾期金额: ").append(recordInfo.getOverdueAmount()).append("元")
                            .append(", 解决周期: ").append(recordInfo.getSettlementPeriod()).append("天")
                            .append(", 处理方案: ").append(recordInfo.getTreatmentPlan());
                    if (i < count - 1) {
                        sb.append("; ");
                    }
                }
                // 将拼接结果放入返回对象中
                jsonObject.put("overdueRecordSummary", sb.toString());
            }
            ResponseVo responseVo = new ResponseVo(AppRspCodeConstant.SUCCESS, jsonObject);
            return ApiUtil.dealReturnJson(responseVo, appEncrypt, startdate, uri);
        } catch (Exception e) {
            // 记录异常日志
            e.printStackTrace();
            // 返回错误响应
            ResponseVo responseVo = new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION, "系统异常");
            return ApiUtil.dealReturnJson(responseVo, appEncrypt, startdate, uri);
        }
    }
    @RequestMapping(value = "/getQrCode", method = {RequestMethod.POST, RequestMethod.GET})
    public String getQrCode(@RequestAttribute(value = "tranData") JSONObject tranData,
                            @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                            @RequestAttribute(value = "channelBaseInfo") ChannelBaseInfo channelBaseInfo,
                            @RequestAttribute(value = "applicationInfo") ApplicationInfo applicationInfo,
                            @RequestAttribute(value = "uri") String uri,
                            @RequestAttribute(value = "startDate") Date startdate,
                            @RequestAttribute(value = "ip") String ip) {


        ResponseVo responseVo = appletConfigInfoService.selectAppletByChannelCodeAndType(channelBaseInfo.getChannelCode());
        return ApiUtil.dealReturnJson(responseVo, appEncrypt, startdate, uri);
    }


}
